package com.hefeng.wx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hefeng.common.core.web.domain.AjaxResult;
import com.hefeng.common.redis.service.RedisService;
import com.hefeng.wx.common.StartLoadingParameter;
import com.hefeng.wx.controller.entity.ParamEntity;
import com.hefeng.wx.domain.WxDeviceReport;
import com.hefeng.wx.domain.WxDeviceUser;
import com.hefeng.wx.service.WxDeviceReportService;
import com.hefeng.wx.service.WxDeviceUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/resident/report")
@Api(tags="报警信息Contoller")
public class WxReportContoller {
    @Autowired
    WxDeviceReportService wxDeviceReportService;
    @Autowired
    private RedisService redisService;
    @Autowired
    WxDeviceUserService wxDeviceUserService;
    @RequestMapping(value = "/getReportList",produces = "application/json;charset=UTF-8")
    @ApiOperation("获取用户实时报警信息")
    public AjaxResult getReportList(@RequestBody ParamEntity paramEntity){
        Map<String,Object> map=new HashMap<>();
        List dataList=new ArrayList();
        //1.查询当前用户关联设备信息
        String userId=paramEntity.getUserId();
        LambdaQueryWrapper<WxDeviceUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WxDeviceUser::getUserId,Long.parseLong(userId));
        queryWrapper.eq(WxDeviceUser::getIsDelete,1);
        List<WxDeviceUser> deviceList=wxDeviceUserService.list(queryWrapper);
        deviceList.forEach(dev->{
            WxDeviceReport obj=redisService.getCacheObject(dev.getDeviceCode());
           if (obj!=null){
               obj.setUserId(Long.parseLong(userId));
               dataList.add(obj);
           }
        });
        map.put("reportList",dataList);
    return AjaxResult.success(map);
    }

    /**
     * 方法说明: 获取用户历史报警
     * @param
     * @return null
     * @Author: huzh
     * @Date: 2022/9/16
     */
    @RequestMapping(value = "/getHistoryReporList",produces = "application/json;charset=UTF-8")
    @ApiOperation("获取用户历史报警信息")
    public AjaxResult getHistoryReporList(@RequestBody ParamEntity paramEntity){
        Map<String,Object> map=new HashMap<>();
        Page<WxDeviceReport> pagelist=wxDeviceReportService.paginQuery(paramEntity.getUserId(),paramEntity.getPage(), paramEntity.getPageSize());
        map.put("dataList",pagelist);
        return AjaxResult.success("获取成功！",map);
    }

}
